local COLORS = {GREEN=0x00FF00,RED=0xFF0000,PURPLE=0xFF00FF,CYAN=0x00FFFF,ORANGE=0xFF8800}
local keywordsRed = {"and","break","do","else","elseif","end","false","for","function","goto","if","in","local","nil","not","or","repeat","return","then","true","until","while"}

local highlight = {}
highlight.isLua = false

function highlight.output(line)
  if not highlight.isLua then return {} end
  local matches = {}

  local it = 0
  
  it = 0
  local ss,ee = string.find(line,"%u*$")
  if ss ~= nil then table.insert(matches,{ss,ee,COLORS.ORANGE}) end
  repeat
    local s,e = string.find(line,"%u*[^%a%d]",it+1)
    if s ~= nil then table.insert(matches,{s,e-1,COLORS.ORANGE}) end
    it = e
  until not s
  
  it = 0
  repeat
    local s,e = string.find(line,"%.%a[%a%d]*",it+1)
    if s ~= nil then table.insert(matches,{s,e,COLORS.RED}) end
    it = e
  until not s

  it = 0
  repeat
    local s,e = string.match(line,"function%s?[%a%d%.]*()%(.-()%)",it+1)
    if s ~= nil then table.insert(matches,{s,e,COLORS.RED}) end
    it = e
  until not s

  it = 0
  repeat
    local s,e = string.find(line,"%a[%a%d]-%(",it+1)
    if s ~= nil then table.insert(matches,{s,e,COLORS.CYAN}) end
    it = e
  until not s

  for _,keyword in pairs(keywordsRed) do
    local ss,ee = string.find(line,"^"..keyword.."[^%a%d]")
    if ss ~= nil then table.insert(matches,{ss,ee-1,COLORS.PURPLE}) end
    local ss,ee = string.find(line,"[^%a%d]"..keyword.."$")
    if ss ~= nil then table.insert(matches,{ss+1,ee,COLORS.PURPLE}) end
    it = 0
    repeat
      local s,e = string.find(line,"[^%a%d]"..keyword.."[^%a%d]",it+1)
      if s ~= nil then table.insert(matches,{s+1,e-1,COLORS.PURPLE}) end
      it = e
    until not s
  end

  it = 0
  repeat
    local s,e = string.find(line,"[%(%)%{%}%[%]]",it+1)
    table.insert(matches,{s,e,COLORS.RED})
    it = e
  until not s
  table.remove(matches)

  it = 0
  repeat
    local s,e = string.find(line,"\".-\"",it+1)
    table.insert(matches,{s,e,COLORS.GREEN})
    it = e
  until not s
  table.remove(matches)

  return matches
end

return highlight